package com.jfirer.jsql.starter;

import com.jfirer.jsql.SessionFactory;
import com.jfirer.jsql.metadata.Page;
import com.jfirer.jsql.model.Model;
import com.jfirer.jsql.model.model.QueryModel;
import com.jfirer.jsql.session.SqlSession;
import com.jfirer.jsql.transfer.ResultSetTransfer;

import java.sql.Connection;
import java.util.Collection;
import java.util.List;

/**
 * SqlSession模板类，用于Spring环境下的线程安全访问
 */
public class JSqlSessionSpringTemplate implements SqlSession
{
    private final SessionFactory sessionFactory;

    public JSqlSessionSpringTemplate(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public <T> T getMapper(Class<T> mapperClass)
    {
        return sessionFactory.openSession().getMapper(mapperClass);
    }

    @Override
    public <T> void batchInsert(Collection<T> collection, int batchSize)
    {
        SqlSession session = sessionFactory.openSession();
        session.batchInsert(collection, batchSize);
    }

    @Override
    public <T> T findOne(QueryModel model)
    {
        SqlSession session = sessionFactory.openSession();
        return session.findOne(model);
    }

    @Override
    public <T> List<T> findList(QueryModel model)
    {
        SqlSession session = sessionFactory.openSession();
        return session.findList(model);
    }

    @Override
    public Page findListByPage(QueryModel model)
    {
        SqlSession session = sessionFactory.openSession();
        return session.findListByPage(model);
    }

    @Override
    public int count(Model model)
    {
        SqlSession session = sessionFactory.openSession();
        return session.count(model);
    }

    @Override
    public int execute(Model model)
    {
        SqlSession session = sessionFactory.openSession();
        return session.execute(model);
    }

    @Override
    public <T> int save(T entity)
    {
        SqlSession session = sessionFactory.openSession();
        return session.save(entity);
    }

    @Override
    public <T> int update(T entity)
    {
        SqlSession session = sessionFactory.openSession();
        return session.update(entity);
    }

    @Override
    public <T> int insert(T entity)
    {
        SqlSession session = sessionFactory.openSession();
        return session.insert(entity);
    }

    @Override
    public int execute(String sql, List<Object> params)
    {
        SqlSession session = sessionFactory.openSession();
        return session.execute(sql, params);
    }

    @Override
    public String insertReturnPk(String sql, List<Object> params, com.jfirer.jsql.metadata.TableEntityInfo.ColumnInfo pkInfo)
    {
        SqlSession session = sessionFactory.openSession();
        return session.insertReturnPk(sql, params, pkInfo);
    }

    @Override
    public <T> T query(String sql, ResultSetTransfer transfer, List<Object> params)
    {
        SqlSession session = sessionFactory.openSession();
        return session.query(sql, transfer, params);
    }

    @Override
    public <T> List<T> queryList(String sql, ResultSetTransfer transfer, List<Object> params)
    {
        SqlSession session = sessionFactory.openSession();
        return session.queryList(sql, transfer, params);
    }

    @Override
    public void close()
    {
        // 不需要关闭，由Spring管理
    }

    @Override
    public Connection getConnection()
    {
        throw new UnsupportedOperationException("不需要获取实际的数据库连接，避免出现意外操作");
    }
}